package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.Bindable;
import android.databinding.ObservableArrayList;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.PopupActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.WaitActivityResult;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/11/12.
 */

public class StudyModel extends ContextObservable implements WaitActivityResult {
    //    private ObservableArrayList<MenuItem> menus;
    private boolean loading;
    //    private ListBindingAdapter menuList;
//    private VideoItem topVideo;
    private ListBindingAdapter<ClickMenuItem> menus;
    private ListBindingAdapter<VideoItem> videoItems;

    @Bindable
    public ListBindingAdapter<ClickMenuItem> getMenus() {
        return menus;
    }
//
//    @Bindable
//    public VideoItem getTopVideo() {
//        return topVideo;
//    }
//
//    public void setTopVideo(VideoItem topVideo) {
//        this.topVideo = topVideo;
//        this.notifyPropertyChanged(BR.topVideo);
//    }

    @Bindable
    public ListBindingAdapter<VideoItem> getVideoItems() {
        return videoItems;
    }

    public StudyModel(Context context) {
        super(context);
//        this.menus = new ObservableArrayList<MenuItem>();
        List<Integer> layoutList = new ArrayList<Integer>();
        layoutList.add(R.layout.i_click_menu);
        layoutList.add(R.layout.i_first_click_menu);
        this.menus = new ListBindingAdapter<ClickMenuItem>(context, BR.menu, layoutList);
        this.videoItems = new ListBindingAdapter<VideoItem>(context, BR.video);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == HomeActivity.REQUEST_BLOCKRESULT) {
                MenuItem item = new MenuItem(getContext(), 0);
                Bundle bundle = data.getExtras();
                item.setCode(bundle.getString("code"));
                item.setTitle(bundle.getString("title"));
                VideoListModel model = new VideoListModel(getContext());
                model.setLoadType(VideoListModel.LOAD_VIDEO_FROM_MENU);
                model.setMenuItem(item);
                CommonFragment fragment = new CommonFragment();
                fragment.setLayoutId(R.layout.f_freevideos);
                fragment.setVariableId(BR.videos);
                fragment.setDataModel(model);
                HomeActivity.switchFragment((Activity) getContext(), fragment);
            }
        }
    }

    /**
     * 从服务器加载首页数据
     */
    public void load() {
        if (loading) {
            return;
        }
        loading = true;
        final Context context = super.getContext();
        LoadingDialog.getInstance(context).show();
        menus.clear(true);
        videoItems.clear(true);
        notifyDataSetChanged(menus);
        new Thread(new Runnable() {
            @Override
            public void run() {
                List menuList = WebApi.getMenus(context);
                int index = 0;
                for (Object menu : menuList) {
                    LinkedTreeMap map = (LinkedTreeMap) menu;
                    ClickMenuItem item = new ClickMenuItem(context, index == 0 ? R.layout.i_first_click_menu
                            : R.layout.i_click_menu);
                    item.setCode((String) map.get("code"));
                    item.setTitle((String) map.get("title"));
                    menus.add(item);
                    index++;
                }
                notifyDataSetChanged(menus);

                List videoList = WebApi.getIndexVideos(context);
                videoItems.setNotifyOnChange(false);
                try {
                    for (Object menu : videoList) {
                        LinkedTreeMap map = (LinkedTreeMap) menu;
                        VideoItem item = new VideoItem(context, R.layout.i_study);
                        item.setUid(Utils.getIntValue(map.get("uid")));
                        item.setCover((String) map.get("cover"));
                        item.setFreeflag(Integer.valueOf((String) map.get("freeflag")));
                        item.setNotepath((String) map.get("notepath"));
                        item.setPrice(Double.valueOf((String) map.get("price")));
                        item.setSpeaker((String) map.get("speaker"));
                        item.setUploadate(Utils.getDateFromTimestamp(map.get("uploaddate")));
                        item.setVideodesc((String) map.get("videodesc"));
                        item.setVideoid(Integer.valueOf((String) map.get("videoid")));
                        item.setVideoname((String) map.get("videoname"));
                        item.setVideopath((String) map.get("videopath"));
                        item.setVideotype((String) map.get("videotype"));

                        videoItems.add(item);
                    }
                    notifyDataSetChanged(videoItems);
                } catch (Exception ex) {
                    showToast("解析时间时发生错误");
                }
                loading = false;
                LoadingDialog.getInstance(context).dismiss();
            }
        }).start();
    }

    public void clickFree(View view) {

        VideoListModel model = new VideoListModel(getContext());
        model.setLoadType(VideoListModel.LOAD_FREE_VIDEO);
        CommonFragment fragment = new CommonFragment();
        fragment.setLayoutId(R.layout.f_freevideos);
        fragment.setVariableId(BR.videos);
        fragment.setDataModel(model);
        HomeActivity.switchFragment((Activity) getContext(), fragment);
    }

    public void onClickAll(View view) {
        Intent intent = new Intent(getContext(), PopupActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("layoutId", R.layout.v_dialog_filter);
        bundle.putInt("variableId", BR.all);
        intent.putExtras(bundle);

        ListBindingAdapter<MenuItem> menuItems = new ListBindingAdapter<MenuItem>(getContext(), BR.menu);

        for (int i = 0; i < menus.getCount(); i++) {
            MenuItem item = menus.getItem(i);
            menuItems.add(new ClickMenuItem(getContext(), R.layout.i_menu, item.getTitle(), item.getCode()));
        }

        AllMenuModel model = new AllMenuModel(getContext(), menuItems);
        PopupActivity.setDataModel(model);
        ((Activity) getContext()).startActivityForResult(intent, HomeActivity.REQUEST_BLOCKRESULT);
    }
}
